package bookshop.controller.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.domain.Book;
import bookshop.domain.Customer;
import bookshop.domain.TempStore;
import bookshop.service.BookService;
import bookshop.service.TempStoreService;
import bookshop.service.helper.TempStoreQueryHelper;
import bookshop.service.impl.BookServiceImpl;
import bookshop.service.impl.TempStoreServiceImpl;
import bookshop.utils.Page;

public class TempStoreServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String act=request.getParameter("act");
		
		
		if("add_temp".equals(act)){
			if(request.getSession().getAttribute("loginedCustomer")==null)
			{
				request.getSession().setAttribute("errMsg", "请登录");
				response.sendRedirect("customer?act=toLogin");
			}
			else{
				String bookCode=request.getParameter("bookcode");
				BookService bookService=new BookServiceImpl();
				Book book=bookService.getBookByCode(bookCode);
				
				Customer customer = (Customer)request.getSession().getAttribute("loginedCustomer");
				
				
				
				TempStore ts = new TempStore();
				ts.setBook(book);
				ts.setCustomer(customer); 
				ts.setStoreTime(new java.util.Date());
				
				
				TempStoreService tempstoreService = new TempStoreServiceImpl();
				tempstoreService.saveTempStore(ts);
				
				response.sendRedirect("tempstore?act=loadall");
			}
			
		}
		else if("loadall".equals(act)){
			if(request.getSession().getAttribute("loginedCustomer")==null)
			{
				request.getSession().setAttribute("errMsg", "请登录");
				response.sendRedirect("customer?act=toLogin");
			}else{
            Customer customer = (Customer)request.getSession().getAttribute("loginedCustomer");			
			TempStoreService tempstoreService =new TempStoreServiceImpl();
			
			List<TempStore> tss = new ArrayList<TempStore>();
			tss = tempstoreService.getBookByCustID(customer.getCustomerId());
			request.setAttribute("tss", tss);
			request.getRequestDispatcher("jsps/front/mydd/store.jsp").forward(request, response);
		}
		}
		else if("remove".equals(act)){
			int Id=Integer.parseInt(request.getParameter("id"));
			//String bookCode=request.getParameter("bookcode");
			
			Customer customer = (Customer)request.getSession().getAttribute("loginedCustomer");	
			TempStoreService tempstoreService =new TempStoreServiceImpl();			
			tempstoreService.removeBook(Id);

			response.sendRedirect("tempstore?act=loadall");
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
